package com.erpanderp.wam.adm.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;

import com.erpanderp.wam.adm.model.entity.Note;
import com.erpanderp.wam.adm.model.service.NoteDAO;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;


@ManagedBean
@ViewScoped
public class CreateNotesBean implements Serializable{

	private static final long serialVersionUID = 1L;
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	private WAMUser wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
	private TimeZone timeZone = TimeZone.getDefault();

	private Note note=new Note();
	private NoteDAO noteDAO=new NoteDAO();
	
	private List<Note> activeNotes = new ArrayList<Note>();
	private List<Note> inActiveNotes = new ArrayList<Note>();
	
	private boolean FORM_VALID;
	private int activeSno=1;
	private int inActiveSno=1;
	private String activeStatus=CommonLookup.STATUS_ACTIVE;
	private String inActiveStatus=CommonLookup.STATUS_INACTIVE;
	
	public CreateNotesBean() {
		activeNotes = noteDAO.getAllNotesByStatus(CommonLookup.STATUS_ACTIVE);
		for(Note note  : activeNotes){
			note.setSno(activeSno);
			activeSno++;
		}
		inActiveNotes = noteDAO.getAllNotesByStatus(CommonLookup.STATUS_INACTIVE);
		for(Note note  : inActiveNotes){
			note.setSno(inActiveSno);
			inActiveSno++;
		}
		
	}
	
	public void createNewNoteAction(){
		validateForm();				
		if(FORM_VALID){
			note.setCreateDate(new Date());
			note.setCreatedBy(wamUser.getEmployee().getFirstName()+" "+wamUser.getEmployee().getLastName());
			note.setStatus(CommonLookup.STATUS_ACTIVE);
			noteDAO.createNote(note);
			note=new Note();
			activeNotes = noteDAO.getAllNotesByStatus(CommonLookup.STATUS_ACTIVE);
			FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_INFO, "You have cretaed new note successfully.", ""));
		}		
	}
	
	public String updateNotesAction() {		
	  try{
			 Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
			  
			  String listType = params.get("notes");
			  System.out.println("==============listType================"+listType);
	
				if(listType.equalsIgnoreCase("activeNotes")){
					for(Note noteInactive: activeNotes){
						if(noteInactive.getStatus().equalsIgnoreCase(CommonLookup.STATUS_INACTIVE)){
							noteInactive.setDisplayOrder(null);
						}						
					}
					noteDAO.udateNotes(activeNotes);			
					return "createnotes";
				}else if(listType.equalsIgnoreCase("inActiveNotes")){					
					noteDAO.udateNotes(inActiveNotes);			
					return "createnotes";
				}else{
					return null;
				}		
			
		}catch (Exception e1) {
			e1.printStackTrace();
			return null;
		}
		
	}
	public void confirmationAction(ActionEvent e){
		validateForm();	
		if(FORM_VALID){	
			RequestContext.getCurrentInstance().execute("confirmation.show();");
		}else {
			RequestContext.getCurrentInstance().execute("confirmation.hide();");
		}
    }
	public void confirmationActiveNotesAction(ActionEvent e){
		
			RequestContext.getCurrentInstance().execute("confirmationActiveNotes.show();");
	
    }	
	public void confirmationInActiveNotesAction(ActionEvent e){
	
			RequestContext.getCurrentInstance().execute("confirmationInActiveNotes.show();");
		
    }	
	public void validateForm(){
		FORM_VALID = true;		
		
	   if(ValidationUtil.isEmpty(note.getNote(), "New Note", null, FacesContext.getCurrentInstance()) != null){
				FORM_VALID = false;
	   }
	  
		System.out.println("FINAL FORM_VALID============="+FORM_VALID);		
	}


	//SETTERS AND GETTERS
	public WAMUser getWamUser() {
		return wamUser;
	}

	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}

	public Note getNote() {
		return note;
	}

	public void setNote(Note note) {
		this.note = note;
	}

	public NoteDAO getNoteDAO() {
		return noteDAO;
	}

	public void setNoteDAO(NoteDAO noteDAO) {
		this.noteDAO = noteDAO;
	}

	public List<Note> getActiveNotes() {
		return activeNotes;
	}

	public void setActiveNotes(List<Note> activeNotes) {
		this.activeNotes = activeNotes;
	}

	public List<Note> getInActiveNotes() {
		return inActiveNotes;
	}

	public void setInActiveNotes(List<Note> inActiveNotes) {
		this.inActiveNotes = inActiveNotes;
	}

	
	public int getActiveSno() {
		return activeSno;
	}

	public void setActiveSno(int activeSno) {
		this.activeSno = activeSno;
	}

	public int getInActiveSno() {
		return inActiveSno;
	}

	public void setInActiveSno(int inActiveSno) {
		this.inActiveSno = inActiveSno;
	}

	public TimeZone getTimeZone() {
		return timeZone;
	}

	public void setTimeZone(TimeZone timeZone) {
		this.timeZone = timeZone;
	}

	public String getActiveStatus() {
		return activeStatus;
	}

	public void setActiveStatus(String activeStatus) {
		this.activeStatus = activeStatus;
	}

	public String getInActiveStatus() {
		return inActiveStatus;
	}

	public void setInActiveStatus(String inActiveStatus) {
		this.inActiveStatus = inActiveStatus;
	}

}
